import urllib2,re,time

def main():
    """main method"""
    data = open('allboard.xml').read()
    ex = re.compile(r'title=\'([^\s]+)\'')
    l = re.findall(ex,data)
    print l
    mapping = fetchBIDs(l)
    open('bidmapping.txt','w').write('\n'.join('%s %d' % (k,v) for (k,v) in mapping.items()))

def fetchBIDs(li):
    dict = {}
    ex = re.compile(r'bid=\'(\d+)\'')
    counter = 1
    for board in li:
        try:
            data = urllib2.urlopen('http://bbs.fudan.edu.cn/bbs/doc?board=%s' % board).read()
        except:
            print 'oops' + board
            continue
        l = re.findall(ex,data)
        dict[board] = int(len(l) > 0 and l[0] or -1)
        print dict[board]
        counter += 1
        if counter % 20 == 0:
            time.sleep(1)
    return dict

def generatecode():
    """generate code for BID of the BBS"""
    lines = open('bidmapping.txt').readlines();
    codes = []
    for line in lines:
        codes.append('@"%s",@"%s",' % (line.split()[1],line.split()[0]))
    print '\n'.join(code for code in codes)


if __name__ == "__main__":
    generatecode()